package me.zzde.wallpaper.Fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import me.zzde.wallpaper.Adapter.DemoAdapter;
import me.zzde.wallpaper.R;
import me.zzde.wallpaper.model.UserWallpaper;
import me.zzde.wallpaper.utils.ImageUtil;

public class IndexFragment extends Fragment {


    public IndexFragment() {
        // Required empty public constructor
    }

    private RecyclerView recyclerView;
    private DemoAdapter adapter;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_index, container, false);

        recyclerView = view.findViewById(R.id.recylerview);

        recyclerView.setHasFixedSize(true);

        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
        recyclerView.setAdapter(adapter = new DemoAdapter());

//        adapter.replaceAll(getData());

        getData();

        return view;
    }

    public void getData() {
        final ArrayList<String> list = new ArrayList<>();
        final Gson gson = new Gson();
        OkHttpClient okHttpClient = new OkHttpClient();
        final String baseUrl = "http://172.20.213.86:8080/";
        final Request request = new Request.Builder().get().url(baseUrl + "mobile/").build();

        Call call = okHttpClient.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                Log.i("ApiServer", "onFailure: " + "error");
            }

            @Override
            public void onResponse(Response response) throws IOException {
                if (response.isSuccessful()) {
                    try {
                        final String result = response.body().string();
                        List<UserWallpaper> us = gson.fromJson(result, new TypeToken<List<UserWallpaper>>() {}.getType());
                        for (UserWallpaper u:us
                             ) {
                            list.add(baseUrl+u.getWallpaperOrigin());
                        }
                        Log.i("lists", list.toString());
                        adapter.replaceAll(list);

                    } catch (Exception e) {
                        Log.e("ApiServer", "Exception = " + e);
                    }
                }else{
                    Log.i("ApiServer", "onResponse: " + "error");
                }
            }
        });

    }


}
